#include "wxCVFrame.h"




// Define a constructor for my canvas
MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
        wxScrolledWindow(parent, wxID_ANY, pos, size)
{
    this->Connect( wxEVT_PAINT, wxPaintEventHandler( MyCanvas::OnPaint ) );
    g_TestBitmap = (wxBitmap *) NULL;

}

// Define the repainting behaviour
void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
{


    wxPaintDC dc(this);
    PrepareDC( dc );
    dc.SetPen(* wxRED_PEN);



    if ( g_TestBitmap && g_TestBitmap->Ok() )
    {
        wxMemoryDC memDC;
        if ( g_TestBitmap->GetPalette() )
        {
            memDC.SetPalette(* g_TestBitmap->GetPalette());
            dc.SetPalette(* g_TestBitmap->GetPalette());
        }
        memDC.SelectObject(* g_TestBitmap);

        // Normal, non-transparent blitting
        dc.Blit(0, 0, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, false);
        printf("image height is %d and image width is %d \n",g_TestBitmap->GetHeight(), g_TestBitmap->GetWidth());
        memDC.SelectObject(wxNullBitmap);
    }


}

// Define a destructor
MyCanvas::~MyCanvas()
{
    this->Disconnect( wxEVT_PAINT, wxPaintEventHandler( MyCanvas::OnPaint) );
}




wxCVFrame::wxCVFrame( wxWindow* parent )
        :
        MyMainFrame( parent )
{
    ///////////////////////// variables
    image1fname=wxT("");
    image2fname=wxT("");
    image1loaded=false;
    image2loaded=false;
    imagecorrloaded=false;

    canvas  = new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100));
    Center();
    canvas->SetScrollbars( 10, 10, 50, 275 );
}

wxCVFrame::~wxCVFrame()
{
    if (canvas->g_TestBitmap)
    {
        delete canvas->g_TestBitmap;
        canvas->g_TestBitmap = (wxBitmap *) NULL;
    }


}
void wxCVFrame::OpenImagePair( wxCommandEvent& event )
{
    DoubleImageDialog fileopendouble(this);


    if (fileopendouble.ShowModal() == wxID_OK)
    {
        image1fname = fileopendouble.m_textCtrl_im1->GetValue();
        image2fname = fileopendouble.m_textCtrl_im2->GetValue();

        char tempbuf1[100];
        char tempbuf2[100];
        strcpy( tempbuf1, (const char*)image1fname.mb_str(wxConvUTF8) );
        strcpy( tempbuf2, (const char*)image2fname.mb_str(wxConvUTF8) );
        img1.openim(tempbuf1);
        img2.openim(tempbuf2);
        printf("file 1 is %s and file 2 is %s \n",tempbuf1,tempbuf2);

    }
   //synthesize the correlation image
  imgcorr.init(&img1,&img2);

    if ( canvas->g_TestBitmap )
    {
        delete canvas->g_TestBitmap;
        canvas->g_TestBitmap=NULL;
    }

    imgcorr.HRImagetowxImage(&canvas->g_TestBitmap);
imgcorr.displayImage();
    //window adjustment
    int theight,twidth;
    GetClientSize(&twidth,&theight);
    if(twidth< canvas->g_TestBitmap->GetWidth() || theight<canvas->g_TestBitmap->GetHeight())
    SetClientSize(canvas->g_TestBitmap->GetWidth(), canvas->g_TestBitmap->GetHeight());

    if (!canvas->g_TestBitmap->Ok())
    {
        delete canvas->g_TestBitmap;
        canvas->g_TestBitmap = (wxBitmap *) NULL;
    }

    canvas->Refresh();
}

void wxCVFrame::CloseImagePair( wxCommandEvent& event )
{
    // TODO: Implement CloseImagePair
}

void wxCVFrame::Exit( wxCommandEvent& event )
{
    wxTheApp->Exit();
}

void wxCVFrame::ShowImagesWX( wxCommandEvent& event )
{
    // TODO: Implement ShowImagesWX
}

void wxCVFrame::ShowImagesCV( wxCommandEvent& event )
{
    img1.displayImage();
    img2.displayImage();
}

void wxCVFrame::ReduceContrast( wxCommandEvent& event )
{
    img1.divideAll(2);
    img2.divideAll(2);
}

void wxCVFrame::EdgeDetect( wxCommandEvent& event )
{
    // TODO: Implement EdgeDetect
}

void wxCVFrame::OnAbout( wxCommandEvent& event )
{
    wxMessageBox( wxT("wxcv") );
}
void wxCVFrame::OnMotion( wxMouseEvent& event )
{
//    if (event.Dragging())
//    {
//        wxClientDC dc(this);
//        wxPen pen(*wxRED, 1); // red pen of width 1
//        dc.SetPen(pen);
//        dc.DrawPoint(event.GetPosition());
//        dc.SetPen(wxNullPen);
//    }

}
